********************************************************************************************
*This file cleans enrollment in institutions over the whole year (12 months) from the IPEDS*
********************************************************************************************

*** create dta files from csv, labeling
cd "$raw_data_education/12MonthsEnrollment"

foreach i in 1993 1994 1995 1999 2000 {
cap do "$do_clean_education/IPEDS - do/do 12MonthsEnrollment/ic`i'_b.do"
keep unitid tostucu tostucg tostucp
rename tostucu undergraduateEnrollment
rename tostucg graduateEnrollment 
rename tostucp firstprofessionalEnrollment
egen total12Months = rsum (undergraduateEnrollment graduateEnrollment firstprofessionalEnrollment)
replace total12Months = . if mi(undergraduateEnrollment) & mi(graduateEnrollment) & mi(firstprofessionalEnrollment)
gen year = `i'
save "$clean_data_education/12MonthEnrollment_`i'.dta", replace
clear
}
************************************************************

foreach i in 2001 {
cap do "$do_clean_education/IPEDS - do/do 12MonthsEnrollment/ef`i'd.do"
keep unitid lstudy fyrace17
gen graduateEnrollment = fyrace17 if lstudy == 3
gen firstprofessionalEnrollment = fyrace17 if lstudy == 2
replace fyrace17 = 0 if lstudy != 1
rename fyrace17 undergraduateEnrollment
drop lstudy
sort unitid undergraduateEnrollment graduateEnrollment firstprofessionalEnrollment 
collapse (sum) undergraduateEnrollment graduateEnrollment firstprofessionalEnrollment, by(unitid)
egen total12Months = rsum (undergraduateEnrollment graduateEnrollment firstprofessionalEnrollment)
replace total12Months = . if mi(undergraduateEnrollment) & mi(graduateEnrollment) & mi(firstprofessionalEnrollment)
gen year = `i'
save "$clean_data_education/12MonthEnrollment_`i'.dta", replace
clear
}
************************************************************

foreach i in 2006 2007 {
cap do "$do_clean_education/IPEDS - do/do 12MonthsEnrollment/ef`i'd.do"
keep unitid lstudy fyrace24
gen graduateEnrollment = fyrace24 if lstudy == 3
gen firstprofessionalEnrollment = fyrace24 if lstudy == 2
replace fyrace24 = 0 if lstudy != 1 // need this for the sum in collapse
rename fyrace24 undergraduateEnrollment
drop if lstudy == 999
drop lstudy
sort unitid undergraduateEnrollment graduateEnrollment firstprofessionalEnrollment
collapse (sum) undergraduateEnrollment graduateEnrollment firstprofessionalEnrollment, by(unitid)
egen total12Months = rsum (undergraduateEnrollment graduateEnrollment firstprofessionalEnrollment)
replace total12Months = . if mi(undergraduateEnrollment) & mi(graduateEnrollment) & mi(firstprofessionalEnrollment)
gen year = `i'
save "$clean_data_education/12MonthEnrollment_`i'.dta", replace
clear
}
************************************************************

foreach i in 2008 2013 2014 2015 {
cap do "$do_clean_education/IPEDS - do/do 12MonthsEnrollment/ef`i'd.do"
keep unitid lstudy efytotlt
gen graduateEnrollment = efytotlt if lstudy == 3
replace graduateEnrollment = 0 if graduateEnrollment == .
gen firstprofessionalEnrollment = efytotlt if lstudy == 2
replace firstprofessionalEnrollment = 0 if firstprofessionalEnrollment == .
replace efytotlt = 0 if lstudy != 1 // need this for the sum in collapse
rename efytotlt undergraduateEnrollment
drop if lstudy == 999
drop lstudy
sort unitid undergraduateEnrollment graduateEnrollment firstprofessionalEnrollment 
collapse (sum) undergraduateEnrollment graduateEnrollment firstprofessionalEnrollment, by(unitid)
egen total12Months = rsum (undergraduateEnrollment graduateEnrollment firstprofessionalEnrollment)
replace total12Months = . if mi(undergraduateEnrollment) & mi(graduateEnrollment) & mi(firstprofessionalEnrollment)
gen year = `i'
save "$clean_data_education/12MonthEnrollment_`i'.dta", replace
clear
}
************************************************************


* append
use "$clean_data_education/12MonthEnrollment_1993.dta", clear
foreach i in 1994 1995 1999 2000 2001 2006 2007 2008 2013 2014 2015 {
append using "$clean_data_education/12MonthEnrollment_`i'.dta"
rm "$clean_data_education/12MonthEnrollment_`i'.dta"
}
rm "$clean_data_education/12MonthEnrollment_1993.dta"

* if at least one is != 0 than the missing must be 0
replace undergraduateEnrollment = 0 if mi(undergraduateEnrollment) & !mi(total12Months)
replace graduateEnrollment = 0 if mi(graduateEnrollment) & !mi(total12Months)
replace firstprofessionalEnrollment = 0 if mi(firstprofessionalEnrollment) & !mi(total12Months)
label variable undergraduateEnrollment "12-months unduplicated headcount"
label variable graduateEnrollment "12-months unduplicated headcount"
label variable firstprofessionalEnrollment "12-months unduplicated headcount"
label variable total12Months "12-months unduplicated headcount" 

* final cleanings
do "$do_clean_education/clean institutions - id.do"

*** Build three year averages
* year group
gen yearGroup = 0
replace yearGroup = 1990 if year <= 1991 
replace yearGroup = 1994 if year >= 1993 & year <= 1995
replace yearGroup = 2000 if year >= 1999 & year <= 2001
replace yearGroup = 2007 if year >= 2006 & year <= 2008
replace yearGroup = 2014 if year >= 2013

**********************************************************
* Run it all together, save labels and collapse
foreach v of var * {
local l`v' : variable label `v'
	if `"`l`v''"' == "" {
	local l`v' "`v'"
  	}
}
collapse (mean) undergraduateEnrollment graduateEnrollment firstprofessionalEnrollment total12Months, by(unitid yearGroup)

foreach v of var * {
label var `v' "`l`v''"
}
**********************************************************

save "$clean_data_education/12MonthsEnrollment.dta", replace




